#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import TYPE_CHECKING

from sqlalchemy import Column, ForeignKey, Integer, String, DateTime, text, BigInteger, DECIMAL, TIMESTAMP
from sqlalchemy.orm import relationship

from app.db.base_class import Base


class Bar(Base):
    __tablename__ = 'tb_bar'

    id = Column(BigInteger, primary_key=True)
    marketType = Column('market_type', String(2))
    instrumentId = Column('instrument_id', Integer)
    frequency = Column(String(20))
    low = Column(DECIMAL(16, 10))
    high = Column(DECIMAL(16, 10))
    open = Column(DECIMAL(16, 10))
    close = Column(DECIMAL(16, 10))
    volume = Column(DECIMAL(24, 10))
    amount = Column(DECIMAL(24, 10))
    preClose = Column('pre_close', DECIMAL(16, 10))
    change = Column(DECIMAL(16, 10))
    pctChange = Column('pct_change', DECIMAL(12, 6))
    turn = Column(DECIMAL(12, 6))
    tradeStatus = Column('trade_status', Integer)
    adjustFlag = Column('adjust_flag', Integer)
    isSt = Column('is_st', Integer, server_default=text('0'))
    peTtm = Column('pe_ttm', DECIMAL(16, 6))
    pbMrq = Column('pb_mrq', DECIMAL(16, 6))
    psTtm = Column('ps_ttm', DECIMAL(16, 6))
    pcfNcfTtm = Column('pcf_ncf_ttm', DECIMAL(16, 6))
    factor = Column('factor', DECIMAL(16, 6))
    barDate = Column('bar_date', TIMESTAMP)
    createdDate = Column('created_date', TIMESTAMP, server_default=text('CURRENT_TIMESTAMP'))
    updatedDate = Column('updated_date', TIMESTAMP,
                         server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))

    def __getitem__(self, item):
        return self.__getattribute__(item)
